package iSOCK::Controller::Datasheet::Admin;

use Try::Tiny;

use Moose;
use namespace::autoclean;

BEGIN { extends 'iSOCK::Controller::Root' }

=head1 NAME

iSOCK::Controller::Datasheet::Admin - Datasheet Admin Controller for iSOCK

=head1 DESCRIPTION

[enter your description here]

=head1 METHODS

=cut

sub index :Path :Args(0) {
  my ( $self, $c ) = @_;

  if ( my $user_r = $c->model ( 'App' )->user_r ( $c ) ) {
    my $parameter = $c->request->parameters;

    if ( $user_r->bofh || $user_r->assert_user_role ( 'can_admin_datasheets' ) ) {
      if ( $c->request->method eq 'POST' && $parameter->{form_admin_submit} ) {
        my $datasheet_id = $parameter->{neo_datasheet_id};
        
        if ( my $repository_id = $parameter->{neo_repository_id} ) {
          if ( $c->model ( 'App::Datasheet' )->find ( $datasheet_id ) ) {
            $c->stash ( action_failed => 'ID already exists!' );
          }
          else {
            if ( my $repository_r = $c->model ( 'App::Repository' )->find ( $repository_id ) ) {
              try {
                {
                  my $dropzone = $self->session_path ( $c ) . '/dropzone';

                  system "rm -rf $dropzone" if -d $dropzone;

                  system (
                    "svn checkout --non-interactive --depth empty"
                  . ( $repository_r->login    ? " --username " . $repository_r->login    : "" )
                  . ( $repository_r->password ? " --password " . $repository_r->password : "" )
                  . " '" . $repository_r->url . "'"
                  . " $dropzone"
                  ) == 0 or die "Could not checkout " . $repository_r->url . " URL!";

                  system "mkdir -p $dropzone/$datasheet_id/{trunk,tags,branches}";
                  
                  system ( "cd $dropzone ; svn add $datasheet_id" ) == 0
                    or die "Could not add datasheet!";
                  system ( "cd $dropzone ; svn commit --non-interactive -m 'creation (on behalf of " . $user_r->id . ")'" ) == 0
                    or die "Could not commit!";
                }
                
                $c->stash ( action_succeeded => 'Created.' );
              }
              catch {
                ( my $why = $_ ) =~ s/ at \/.+\.$//;
                
                $c->stash ( action_failed => $why );
              };
            }
            else {
              $c->stash ( action_failed => "Unknown $repository_id repository!" );
            }
          }
        }
        else {
          $c->stash ( action_failed => 'Missing repository_id parameter!' );
        }
      }
      
      $c->stash (
        datasheet_rs  => $c->model ( 'App::Datasheet'  ),
        user_rs       => $c->model ( 'App::User'       ),
        repository_rs => $c->model ( 'App::Repository' ),
      );
    }
  }
  
  $c->forward ( qw( iSOCK::Controller::Root render_view ) );
}

sub end : ActionClass('RenderView') {}

=head1 AUTHOR

Xavier Caron

=head1 LICENSE

This library is free software, you can redistribute it and/or modify
it under the same terms as Perl itself.

=cut

1;
